package com.tianqu.tms.www.rental.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.RentalInfoDto;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.controller.RentalContractSearchAddResultController;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同查询/修改-添加确认 action
 * @author mufeiyu
 *
 */
public class RentalContractSearchAddResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(RentalContractSearchAddResultAction.class);
	private static final String OFF="off";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		
		RentalContractAddInputForm rentalForm = (RentalContractAddInputForm) uc.getInputForm();

		if ((!StringUtil.isNullOrBlank(rentalForm.getStartDate()))&&(!StringUtil.isNullOrBlank(rentalForm.getEndDate()))) {	
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			//合同开始的日期
			Date startDate = formatDate.parse(rentalForm.getStartDate());
			//合同结束的日期				
			Date endDate = formatDate.parse(rentalForm.getEndDate());
			// 比较
			if (endDate.compareTo(startDate) < 0) {
				String[] param = {"合同结束日期","合同开始日期"};
				setErrorsToRequest(request, TmsConstants.U0036, param); //合同结束日期比合同开始日期小时
				return mapping.getInputForward();
			}
		}

		if(!StringUtil.isNullOrBlank(rentalForm.getLocationSerial())){
			
			//租位编号是否存在check
			RentalLocationInfo locationInfo = RentalLocationInfo.getInstance();
			if(!locationInfo.isExistLocationInfoBySerial(rentalForm.getLocationSerial())){
				setErrorsToRequest(request, TmsConstants.U9007, null); //租位编号不存在，无法进行保存。				
				return mapping.getInputForward();
			}
			
			//同一个租位的租期是否重叠check
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			//合同开始的日期
			Date startDate = formatDate.parse(rentalForm.getStartDate());
			
			RentalContractInfo contractInfo = RentalContractInfo.getInstance();
			List<RentalInfoDto> rentalInfoDtoList = contractInfo.checkLocationStartDate(rentalForm.getLocationSerial());
			boolean checkFlg = false;
			if (rentalInfoDtoList != null && rentalInfoDtoList.size() > 0) {
				for (int i=0;i < rentalInfoDtoList.size();i++) {
					RentalInfoDto rentalInfoDto = new RentalInfoDto();
					rentalInfoDto = rentalInfoDtoList.get(i);
					Date oldStartDate = rentalInfoDto.getStartDate();
					Date oldEndDate = rentalInfoDto.getEndDate();
					if((!rentalInfoDto.getSerial().equals(rentalForm.getSerial()))
							&& startDate.compareTo(oldStartDate) >= 0 && startDate.compareTo(oldEndDate) <= 0){
						checkFlg = true;				
					}
				}
			}

			if (checkFlg) {
				setErrorsToRequest(request, TmsConstants.U9009, null); //合同结束日期比合同开始日期小时
				return mapping.getInputForward();
			}
			
		}
				
		RentalContractSearchAddResultController controller = new RentalContractSearchAddResultController();
		String sysPath = request.getSession().getServletContext().getRealPath(
		"/").replace("/tmsweb", "");
		uc.setAttribute("sysPath", sysPath);
		controller.execute(uc);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}